package gaishocron.mail;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

public class SimpleMailSender {

    private String subject;
    private String text;
    private String from;
    private String fromName;
    
    private String charset="ISO-2022-JP";

    public void send(List<String> toAddresses, List<String> ccAddresses,
            List<String> bccAddresses) 
            throws UnsupportedEncodingException, MessagingException{
        
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        Message msg = new MimeMessage(session);
        
        msg.setFrom(new InternetAddress(from,fromName));

        if(ccAddresses != null){
            for (String cc : ccAddresses) {
                msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            }
        }

        if(bccAddresses != null){
            for (String bcc : bccAddresses) {
                msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
            }
        }
        
        msg.setSubject(MimeUtility.encodeText(subject, charset, "B"));
        msg.setText(text);
        
        for (String to : toAddresses) {
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            javax.mail.Transport.send(msg);
        }
        
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getFromName() {
        return fromName;
    }

    public void setFromName(String fromName) {
        this.fromName = fromName;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }
    
    
    
}
